package u1.Client;


import novelRead.common.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;

public class MenuClient {
    private static Scanner in = new Scanner(System.in);

    public static boolean login(User user, Response response) {
        int n=0;
        login:
        while (true) {
            while (true) {
                System.out.println("*******************************");
                System.out.println("***********书虫阅读器***********");
                System.out.println("1.登录     2.注册      3.退出程序");
                try {
                    n = in.nextInt();
                    break;
                } catch (Exception e) {
                    in.next();
                    System.out.println("输入非法字符");
                    continue;
                }
            }
            switch (n) {
                case 1:
                    while (true) {
                        user = new User();
                        System.out.println("输入账号");
                        user.setAccount(in.next());
                        System.out.println("输入密码");
                        user.setPassword(in.next());
                        if (NovelReadClient.login(user).isSucess()) {
                            System.out.println("登录成功");
                            return true;
                        } else System.out.println("登录失败");
                        System.out.println("1.继续登录  2.退出登录");
                        while (true) {
                            try {
                                n = in.nextInt();
                                break;
                            } catch (Exception e) {
                                in.next();
                                System.out.println("输入非法字符");
                                continue;
                            }
                        }
                        if (n == 2) {
                            break ;
                        }
                    }
                    break;
                case 2:
                    while (true) {
                        user = new User();
                        System.out.println("输入注册账号");
                        user.setAccount(in.next());
                        System.out.println("输入密码");
                        user.setPassword(in.next());
                        System.out.println("重复输入密码");
                        String password = in.next();
                        if (!password.equals(user.getPassword())) {
                            System.out.println("两次密码不一致");
                            continue;
                        }
                        break;
                    }
                    response = NovelReadClient.register(user);
                    if (response.isSucess()) {
                        System.out.println("注册成功");
                        break login;
                    } else System.out.println("注册失败");
                    System.out.println(response.get("register"));
                    break;
                default:
                    System.exit(0);
            }
        }
        return false;
    }

    public static void downloadMenu(Book book) {
        System.out.println("请输入下载路径");
        String path = in.next();
        Response download = NovelReadClient.download(book);
        if (download.isSucess()) {
            book = (Book) download.get("download");
            book.setPath(path + "/" + book.getTitle());
            NovelReadClient.saveBookLocal(book);
            System.out.println("保存成功");
        } else System.out.println(download.getMsg());

    }
    public static void menuType(User user, Response response) {
        Response maxType = NovelReadClient.maxType();
        if (maxType.isSucess()) {
            Object o = maxType.get("maxType");
            System.out.println(o.toString());
            Type type = null;
            while (true) {
                System.out.println("玄幻,科幻,同人,军事,言情,都市,穿越,重生,历史,无限流");
                System.out.println("请选择你要看的类型");
                try {
                    type = Type.valueOf(in.next());
                    break;
                } catch (Exception e) {
                    System.out.println("请输入规定的类型");
                    continue;
                }
            }
            Response minType = NovelReadClient.minType(type.toString());
            if (minType.isSucess()) {
                ArrayList<Book> bookList = (ArrayList<Book>) minType.get("minType");
                for (int i = 1; i <=bookList.size() ; i++) {
                    System.out.println(i+":"+bookList.get(i-1));
                }
                System.out.println("输入你选择的小说序号");
                int i = in.nextInt();
                Book book = bookList.get(i - 1);
                System.out.println("1.在线阅读  2.下载到本地  请选择");
                int n = in.nextInt();
                if (n == 1) {
                    int page = 1;
                    while (true) {
                        Response readOnline = NovelReadClient.readOnline(book, page);
                        if (readOnline.isSucess()) {
                            book = (Book) readOnline.get("readOnline");
                            System.out.println(book.getContent());
                            System.out.println("1.下一页 2.跳页 3.下载  4.返回上一级");
                            n = in.nextInt();
                            if (n == 1) {
                                page = page + 1;
                                continue;
                            }
                            if (n == 2) {
                                System.out.println("输入要跳的页数");
                                page = in.nextInt();
                                continue;
                            }
                            if (n == 3) {
                                MenuClient.downloadMenu(book);
                                break;
                            }
                        }else System.out.println(readOnline.getMsg());
                        break;
                    }
                } else if (n == 2) {
                    MenuClient.downloadMenu(book);
                }
            } else System.out.println(minType.getMsg());
        } else System.out.println(maxType.getMsg());

    }

    public static void uploadMenu(){
        System.out.println("请输入你要上传的小说书名");
        String title = in.next();
        System.out.println("请输入要上传的书的地址");
        String path = in.next();
        System.out.println("请输入要上传的书的作者");
        String author = in.next();
        Book book = new Book(title, path, author);
        while (true) {
            System.out.println("玄幻,科幻,同人,军事,言情,都市,穿越,重生,历史,无限流");
            System.out.println("请输入要上传的书的类别");
            Type type = null;
            try {
                type = Type.valueOf(in.next());
            } catch (Exception e) {
                System.out.println("请输入规定的类型");
                continue;
            }
            book.addType(type.toString());
            System.out.println(book.getType());
                break;
        }
        book.readFile();
        book.readcontent();
        Response upload = NovelReadClient.upload(book);
        if (upload.isSucess()) {
            System.out.println("上传成功");
        } else System.out.println(upload.getMsg());
    }

}
